package com.cloud.logger.zchain.zchainserver.service;

import com.cloud.logger.zchain.core.common.base.ResultMessage;
import com.cloud.logger.zchain.core.common.constant.ResultCodeConstant;
import com.cloud.logger.zchain.core.common.constant.UUIDGenerator;
import com.cloud.logger.zchain.core.exception.BusinessException;
import com.cloud.logger.zchain.zchainserver.base.GlobalConstants;
import com.cloud.logger.zchain.zchainserver.entity.ZchainUser;
import com.cloud.logger.zchain.zchainserver.mongo.ZchianUserRepository;
import com.cloud.logger.zchain.zchainserver.util.GlobalMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;


/**
 * 浙江卓锐科技股份有限公司
 *
 * @author zhengyt
 * @create 2017/12/19
 * @description
 */
@Service
public class LoginService {

    @Resource
    private ZchianUserRepository userRepository;

    private static String USERNAME_OR_PWD_ERROR = "用户名或密码错误";

    private static int COOKIE_MAX_AGE = 1200;

    private static String NAME = "name";

    private static String PASSWORD = "passWord";

    private static String PHONE = "phone";

    private static String CREATE_TIME = "createTime";

    private static String VALID_TIME = "1200";


    public ResultMessage userLogin(String userName,String passWord,HttpServletResponse response) throws BusinessException {
        ResultMessage resultMessage = new ResultMessage();

        ZchainUser user = userRepository.findByUserNameAndPassword(userName,passWord);
        if(user == null){
            throw new BusinessException(USERNAME_OR_PWD_ERROR);
        }

        String token = UUIDGenerator.getUuid();
        Map<String,String> userInfoMap = new ConcurrentHashMap<String,String>();
        userInfoMap.put(GlobalConstants.ID,user.getId());
        userInfoMap.put(GlobalConstants.USER_STATUS,user.getStatus().getName());
        userInfoMap.put(NAME,user.getUserName());
        userInfoMap.put(PASSWORD,user.getPassword());
        userInfoMap.put(PHONE,user.getPhone());
        userInfoMap.put(CREATE_TIME,String.valueOf(new Date().getTime()));
        userInfoMap.put(GlobalConstants.VALID_TIME,VALID_TIME);
        GlobalMap.putUserMap(token,userInfoMap);

        //cookie存储用户名
        Cookie cookie = new Cookie(GlobalConstants.COOKIE_NAME,token);
        cookie.setMaxAge(COOKIE_MAX_AGE);
        cookie.setPath("/");
        response.addCookie(cookie);

        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);

        //返回user信息
        user.setPassword("");
        resultMessage.setContent(user);
        return resultMessage;
    }

    public void cleanUserMap(){
        for (String key : GlobalMap.userMap.keySet()) {
            String validTime = (String) GlobalMap.userMap.get(key).get(GlobalConstants.VALID_TIME);
            String createTime = (String) GlobalMap.userMap.get(key).get(GlobalConstants.CREATE_TIME);
            if(new Date().getTime() - Long.valueOf(createTime) < Long.valueOf(validTime) * 1000){
                continue;
            }
            GlobalMap.userMap.remove(key);
        }
    }


}